<template>
	<view class="page">
		<u-navbar title="风格列表" border @leftClick="$utils.back()" placeholder>
		</u-navbar>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="100">
				<template v-if="!loading">
					<view v-for="item in data" :key="item.id" class="inputBox">
						<view v-if="!item.updateShow">
							{{item.title}}
						</view>
						<u-input v-else v-model="item.title" placeholder='请输入风格' @blur='blur(item)' />
						<view style="display: flex;">
							<u-icon name='edit-pen' @click='edit(item)' />
							<view style="width: 20rpx;"></view>
							<u-icon name='close-circle' color='red' @click='delShow=true;selectItem=item' />
						</view>
					</view>
				</template>
				<template v-else>
					<view v-for="item in 20" style="height: 40rpx;margin-top: 20rpx;" class="Skeleton"></view>
				</template>
			</z-paging>
		</view>
		<view v-if="!loading">
			<u-button @click='addShow=true'>
				新增
			</u-button>
		</view>
		<view>
			<u-modal showCancelButton :show="addShow" title="新增风格" @cancel='cancel' @confirm='add'>
				<u-input v-model="addTitle" placeholder='请输入风格' />
			</u-modal> <u-modal :show="delShow" content='是否确认删除？' showCancelButton @confirm='del(selectItem.id)'
				@cancel='delShow=false'></u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				delShow:false,
				loading: true,
				data: [],
				disabled: false,
				addShow: false,
				addTitle: null,
				selectItem:null
			}
		},
		methods: {
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api_company.Style.List(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.completeByNoMore(res.data.styles.data.map(item => {
						return {
							updateShow: false,
							...item
						}
					}), true)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			},
			del(id) {
				this.delShow=false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api_company.Style.Del(id).then(res => {
					uni.hideLoading()
					this.data = this.data.filter(item => item.id != id)
					this.disabled = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
					this.disabled = false
				})
			},
			blur(item) {
				if(item.title.length>6){
					uni.$u.toast('标题不能超过6个字符')
					return
				}
				uni.$u.toast('编辑成功')
				this.$api_company.Style.Update(item.id, {
					title: item.title,
					description: item.description
				})
				item.updateShow = false
			},
			edit(item) {
				item.updateShow = true
			},
			add() {
				if (!this.addTitle) {
					uni.$u.toast('请输入风格')
					return
				}
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '新增中...'
				})
				this.$api_company.Style.Add({
					title: this.addTitle,
					description: '0'
				}).then(res => {
					uni.hideLoading()
					uni.$u.toast('新增成功')
					this.$refs.paging.reload()
					this.loading = true
					this.cancel()
					this.disabled = false
				}).catch(err => {
					this.disabled = false
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			},
			cancel() {
				this.addTitle = null
				this.addShow = false
			}
		}
	}
</script>

<style scoped lang="less">
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
	}

	.inputBox {
		margin-top: 20rpx;
	}
</style>